package br.com.sistema.views;

import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.event.ShortcutAction.KeyCode;
import com.vaadin.server.Page;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;

public abstract class AbstractManterView extends Window {

	private static final long serialVersionUID = 5370970875423175609L;
	
	private Component component = new GridLayout();
	private HorizontalLayout buttons = new HorizontalLayout();
	private Button confirma = new Button("Confirma");
	
	public void init(){

		buttons.setSpacing(true);
		buttons.addComponent(confirma);
		
		HorizontalLayout h0 = new HorizontalLayout();
		h0.setSpacing(true);
		h0.setMargin(true);
		h0.addComponent(getButtons());

		Panel panel = new Panel();
		panel.setSizeFull();
		panel.setContent(getForm());

		component.setSizeFull();
		((GridLayout) component).setColumns(1);
		((GridLayout) component).setRows(2);
		((GridLayout) component).addComponent(panel, 0, 0);
		((GridLayout) component).addComponent(h0, 0, 1);
		((GridLayout) component).setRowExpandRatio(0, 1);

		this.setContent(component);
		this.setCloseShortcut(KeyCode.ESCAPE, null);
		this.addFocusListener(new FocusListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void focus(FocusEvent event) {
				Page.getCurrent().setTitle(getCaption());
			}
		});
		this.addCloseListener(new CloseListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void windowClose(CloseEvent e) {
				//TODO pegar titulo generico
				Page.getCurrent().setTitle("Sistema");
			}
		});

	}
	
	public abstract Component getForm();
	
	public HorizontalLayout getButtons(){
		return buttons;
	}
	
	public Button getConfirma() {
		return confirma;
	}

	public Component getComponent(){
		return component;
	}
	
}
